<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<title>登录</title>
		<!-- 样 式 文 件 -->
		<link rel="stylesheet" href="component/pear/css/pear.css" />
		<link rel="stylesheet" href="admin/css/other/login.css" />
	</head>
    <!-- 代 码 结 构 -->
	<body background="admin/images/background.svg" style="background-size: cover;">
		<form class="layui-form" action="javascript:void(0);">
			<div class="layui-form-item">
				<img class="logo" src="admin/images/logo.png" />
				<div class="title">Anno Admin</div>
				<div class="desc">
					基于 Pear Admin & Solon的纯注解式 后台管理系统
				</div>
			</div>
			<div class="layui-form-item">
				<input id="account" placeholder="账 户 : admin " lay-verify="required" hover class="layui-input"  />
			</div>
			<div class="layui-form-item">
				<input id="password" type="password" placeholder="密 码 : admin " lay-verify="required" hover class="layui-input"  />
			</div>
			<div class="layui-form-item">
				<input id="captcha" placeholder="验证码 : "  hover  lay-verify="required" class="code layui-input layui-input-inline"  />
				<img id="codeImage"  class="codeImage" />
			</div>
			<div class="layui-form-item">
				<input type="checkbox" name="" title="记住密码" lay-skin="primary" checked>
			</div>
			<div class="layui-form-item">
				<button type="button" class="pear-btn pear-btn-success login" lay-submit lay-filter="login">
					登 入
				</button>
			</div>
		</form>
		<!-- 资 源 引 入 -->
		<script src="component/layui/layui.js"></script>
		<script src="component/pear/pear.js"></script>
		<script src="jquery/jquery.js"></script>
		<script>
			let codeKey = "";
			layui.use(['form', 'button', 'popup'], function() {
				let form = layui.form;
				let button = layui.button;
				let popup = layui.popup;
				// 刷新验证码
				refreshCaptcha();
				// 点击 -> 获取验证码
				$("#codeImage").click(function() {
					refreshCaptcha();
				});
                // 登 录 提 交
				form.on('submit(login)', function(data) {
					/// 验证
					console.log("data", data);
					console.log("codeKey", codeKey);
					/// 登录
					loginByMobileAndPwd(button,popup);
					/// 动画

					return false;
				});
			})

			function refreshCaptcha() {
				$.ajax({
					url: "/system/common/captcha",
					type: "post",
					success: function(data) {
						$("#codeImage").attr("src", data.data.image);
						codeKey = data.data.key;
					}
				})
			}

			function loginByMobileAndPwd(button,popup){
				$.ajax({
					url: "/system/auth/login",
					type: "post",
					contentType: "application/json",
					data:JSON.stringify({
						mobile:$("#account").val(),
						password:$("#password").val(),
						code:$("#captcha").val(),
						codeKey:codeKey
					}),
					success: function(data) {
						if (data.status !== 0) {
							console.log(data)
							layer.msg(data.msg);
							return;
						}
						let token = data.data;
						localStorage.setItem("anno-token",token)
						jumpToIndex(button,popup);
					}
				})
			}

			function jumpToIndex(button,popup){
				button.load({
					elem: '.login',
					time: 300,
					done: function() {
						popup.success("登录成功", function() {
							location.href = "index.html"
						});
					}
				})
			}
		</script>
	</body>
</html>
